package com.alibaba.citrus.ots.platform.enhance.facade.service;

import com.alibaba.citrus.ots.common.annotation.FacadeInvoker;
import com.alibaba.citrus.ots.platform.enhance.repository.DimensionQueryRepository;
import com.alibaba.lattice2.epoch.sdk.model.Result;
import com.epoch.app.otsplatformenhance.api.dimensionquery.service.DimensionQueryService;
import com.epoch.app.otsplatformenhance.dimension.dto.QueryListByPriorityRequest;
import com.epoch.app.otsplatformenhance.dimension.model.Dimension;
import com.epoch.app.otsplatformenhance.enums.DimensionTypeEnum;
import com.epoch.app.otsplatformenhance.model.dto.*;
import com.epoch.app.otsplatformenhance.sdo.DimensionSDO;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Component
public class DimensionQueryServiceImpl implements DimensionQueryService {

    @Resource
    private DimensionQueryRepository dimensionQueryRepository;


    @Override
    @FacadeInvoker
    public Result<DimensionSDO> queryById(DimensionQueryByIdRequest request){
        DimensionSDO dimensionSDO = dimensionQueryRepository.query(request.getId());
        return Result.success(dimensionSDO);
    }

    @Override
    @FacadeInvoker
    public Result<List<DimensionPageQueryResponse>> pageQuery(DimensionPageQueryRequest request){
        Result<List<DimensionPageQueryResponse>> listResult = dimensionQueryRepository.pageQuery(request);
        if (Objects.nonNull(listResult) && !CollectionUtils.isEmpty(listResult.getResult())) {
            return listResult;
        }
        return new Result<>();
    }

    @Override
    public Result<List<QueryListByPriorityServiceResponse>> queryListByPriority(QueryListByPriorityServiceRequest queryListByPriorityServiceRequest) {
        if (StringUtils.isBlank(queryListByPriorityServiceRequest.getStrategyId())) {
            return Result.success(null);
        }
        QueryListByPriorityRequest request = new QueryListByPriorityRequest();
        request.setCode(queryListByPriorityServiceRequest.getCode());
        request.setStrategyId(queryListByPriorityServiceRequest.getStrategyId());
        Result<List<Dimension>> listResult = dimensionQueryRepository.queryListByPriority(request);
        if (CollectionUtils.isNotEmpty(listResult.getResult())) {
            List<QueryListByPriorityServiceResponse> collect = listResult.getResult().stream().map(result -> {
                QueryListByPriorityServiceResponse response = new QueryListByPriorityServiceResponse();
                response.setCode(result.getCode());
                response.setPriority(result.getPriority());
                if (DimensionTypeEnum.DEPARTMENT.getCode().equals(result.getCode()) || DimensionTypeEnum.GOODS.getCode().equals(result.getCode())){
                    response.setValue(result.getOutCode());
                }else {
                    response.setValue(result.getValue());
                }
                response.setValueName(result.getValueName());
                response.setId(result.getId().toString());
                return response;
            }).collect(Collectors.toList());
            return Result.success(collect);
        }
        return Result.success(null);
    }
}
